home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-06-26 | 1.5 KB | 64 lines | [TEXT/CWIE] |
-
- // mail <chelly@eden.com> or surf http://www.eden.com/~chelly for feedback
- // free source code - do whatever you like with it
-
- // universal flat resource file access
-
- // file has table of contents with offsets to resource data, which comes later
-
- #include "resfile_flat.h"
- #include "stream.h"
-
- #include "resfile_flat_item.h"
-
- resfile_flat::resfile_flat( stream* file ) : m_file( *file ) { }
-
- resfile_flat::~resfile_flat() { }
-
- // scan through table of contents to find resource, then read data
- void* resfile_flat::get_resource( long type, int id )
- {
- // go to table of contents, at beginning of file
- m_file.set_mark( 0 );
-
- long data_offset = m_file.get_mac_uint32();
-
- // get number of resources in file
- // stream auto-swaps bytes if necessary
- int resource_count = m_file.get_mac_uint16();
-
- // search through all resource entries
- for ( int count = resource_count; count; --count )
- {
- // read current entry type and id
- uint32 entry_type = m_file.get_mac_uint32();
- uint16 entry_id = m_file.get_mac_uint16();
-
- // do they match?
- if ( entry_type == type && entry_id == id )
- {
- // skip padding
- m_file.skip( 2 );
-
- // get rest of info
- long size = m_file.get_mac_uint32();
- long offset = m_file.get_mac_uint32();
-
- // go to location in file of data
- m_file.set_mark( offset + data_offset );
-
- // read and return resource data
- char* data = new char [size];
- if ( !data ) return nil;
- m_file.get_bytes( data, size );
- return data;
- }
-
- // skip extra info
- m_file.skip( 2 + 4 + 4 );
- }
-
- return nil;
- }
-
-